﻿function cellLayer() {
    this.ui = $("<div></div>");
    this.ui.addClass("cellLayer");
    this.cells = [];
    this._elevation = 1;
    this.elevation = function (p) {
        if (p === undefined) {
            return this._elevation;
        } else {
            this._elevation = p;
        }
    }
    this._resource = "url(\"stone.gif\")";
    this.resource = function (p) {
        if (p === undefined) {
            return this._resource;
        } else {
            this._resource = p;
            this.ui.css("background-image", this._resource);
        }
    }
    this._left = 0;
    this._leftOffset = 0;
    this._x = 0;
    this.x = function (p) {
        if (p === undefined) {
            return this._x;
        } else {
            this._x = p;
            this._left = centerPixelX + (this._x - centerX -0.5) * (cellSize * zoom);
        }
    }
    this._top = 0;
    this._topOffset = 0;
    this._y = 0;
    this.y = function (p) {
        if (p === undefined) {
            return this._y;
        } else {
            this._y = p;
            this._top = centerPixelY + (this._y - centerY - 0.5) * (cellSize * zoom);
        }
    }
    this.render = function (p) {
        var zX = 0;
        var zY = 0;
        if (this._x < centerX) {
            zX = this._x - leftStart;
        } else if (this._x > centerX) {
            zX = rightEnd - this._x;
        } else {
            zX = centerX - leftStart;
        }
        if (this._y < centerY) {
            zY = this._y - topStart;
        } else if (this._y > centerY) {
            zY = bottomEnd - this._y;
        } else {
            zY = centerY - topStart;
        }
        this.ui.css("z-index", zX + zY + this._elevation);
        this.ui.css("width", cellSize * zoom);
        this.ui.css("height", cellSize * zoom);
        if (this._x < centerX) {
            this._leftOffset = -((this._elevation - 1) * cellSize * zoom / fov);
        } else if (this._x > centerX) {
            this._leftOffset = (this._elevation - 1) * cellSize * zoom / fov;
        } else {
            this._leftOffset = -((this._elevation - 1) * cellSize * zoom / fov);
            this.ui.css("width", cellSize * zoom + 2 * (this._elevation - 1) * cellSize * zoom / fov);
        }
        if (this._y < centerY) {
            this._topOffset = -((this._elevation - 1) * cellSize * zoom / fov);
        } else if (this._y > centerY) {
            this._topOffset = (this._elevation - 1) * cellSize * zoom / fov;
        } else {
            this._topOffset = -((this._elevation - 1) * cellSize * zoom / fov);
            this.ui.css("height", cellSize * zoom + 2 * (this._elevation - 1) * cellSize * zoom / fov);
        }
        this.ui.css("background-image", this._resource);
        this.ui.css("left", this._left + this._leftOffset);
        this.ui.css("top", this._top + this._topOffset);
        if (p != undefined) {
            if (p.append === true) {
                $("#mapArea").append(this.ui);
            }
        }
    }
    this.elevation(1);
}